package com.wowudi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wowudi.entity.Meta;
import com.wowudi.entity.dto.Result;
import com.wowudi.mapper.MetaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wowudi.mapper.MenuMapper;
import com.wowudi.entity.Menu;
import com.wowudi.service.MenuService;
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MetaMapper metaMapper;

    /**
    * Description: 返回所有顶级菜单以及下的子菜单<br/>
    * date: 2022-3-14<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Result getMenu() {
        //获取所有顶级菜单项
        List<Map<String, Object>> maps = menuMapper.selectMaps(new QueryWrapper<Menu>().eq("parent_id",0));
        List<Map<String, Object>> menuMap = isChildren(addMeta(maps));
        Result result = new Result();
        result.setData(menuMap);
        return result;
    }




    //获取所有顶级菜单项
    public List<Map<String, Object>> addMeta(List<Map<String, Object>> maps) {
        for (int i = 0; i < maps.size(); i++) {
            //单纯处理一下日期
            String updateTime = maps.get(i).get("update_time").toString();
            String createTime = maps.get(i).get("create_time").toString();
            maps.get(i).put("update_time", updateTime);
            maps.get(i).put("create_time", createTime);


            //添加顶级菜单项的附加数据
            Object metaId = maps.get(i).get("meta_id");
            QueryWrapper<Meta> wrapper = new QueryWrapper<Meta>().eq("meta_id", metaId);
            List<Map<String, Object>> metaMap = metaMapper.selectMaps(wrapper);

            HashMap<String, Object> meta = new HashMap<>();
            for (int j = 0; j < metaMap.size(); j++) {
                Iterator<String> iterator = metaMap.get(j).keySet().iterator();
                while (iterator.hasNext()) {
                    String column = iterator.next();
                    meta.put(column, metaMap.get(j).get(column));
                }
            }
            maps.get(i).put("meta", meta);
        }
        return maps;
    }

    //是否有子菜单
    public List<Map<String, Object>> isChildren(List<Map<String, Object>> maps) {
        for (int i = 0; i < maps.size(); i++) {
            List<Map<String, Object>> mapss = menuMapper.selectMaps(null);
            for (Map<String, Object> objectMap : mapss) {

                int parentId = (int) objectMap.get("parent_id");
                int id = (int) maps.get(i).get("id");
                if (parentId == id) {
                    QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("parent_id", id);
                    List<Map<String, Object>> childrenMap = menuMapper.selectMaps(queryWrapper);
                    for (int k = 0; k < childrenMap.size(); k++) {
                        String uTime = childrenMap.get(k).get("update_time").toString();
                        String cTime = childrenMap.get(k).get("create_time").toString();
                        childrenMap.get(k).put("update_time", uTime);
                        childrenMap.get(k).put("create_time", cTime);
                    }
                    maps.get(i).put("children", addMeta(childrenMap));
                }
            }
        }
        return maps;
    }

}
